function Base64() {

    // private property
    _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    // public method for encoding
    this.encode = function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        input = _utf8_encode(input);
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output +
                _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
        }
        return output;
    }

    // public method for decoding
    this.decode = function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length) {
            enc1 = _keyStr.indexOf(input.charAt(i++));
            enc2 = _keyStr.indexOf(input.charAt(i++));
            enc3 = _keyStr.indexOf(input.charAt(i++));
            enc4 = _keyStr.indexOf(input.charAt(i++));
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = _utf8_decode(output);
        return output;
    }

    // private method for UTF-8 encoding
    _utf8_encode = function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
        return utftext;
    }

    // private method for UTF-8 decoding
    _utf8_decode = function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}
/*
 页面载入,初始执行

 */
function mo_init(initType) {
    moValidatEx();      //扩展jquery验证
    showRequiredTip();      //必选提示
    updateEndPointsEditorName();    //改变入口点组件的name，用于匹配Array组件
    registArrayEditor(); //注册数组类型组件
    registFieldVisiable();  //注册MO的G接口中的可见性
    elasticResize();        //must execute before registOptionalAccessParamStyle()
    registOptionalAccessParamStyle(initType);   //注册MO可选参数展示
    createAccessParaTestBtn(initType); //创建访问参数测试按钮
}


/*
 验证扩展
 */
function moValidatEx() {
    jQuery.validator.addMethod("moRegx", function(value, element, param) {
        return this.optional(element) || param.test(value);
    }, l("mo_validation_wrong_format"));
    jQuery.validator.addMethod("ip", function(value, element, param) {
        var ipreg = /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0[0-9][1-9]|0[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0|0[0-9]{2}|0[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0|0[0-9]{2}|0[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]|0[0-9]{2}|0[0-9])$/;
        return this.optional(element) || ipreg.test(value);
    }, l("mo_validation_wrong_ip"));
    jQuery.validator.addMethod("regexp", function(value, element, param) {
        var reg =new RegExp(param);
        return reg.test(value);
    }, l("mo_validation_wrong_format"));
}


/*
 显示必选项提示信息
 */
function showRequiredTip() {
    var formArr = document.getElementsByTagName("form");
    for (var i = 0; i < formArr.length; i++) {
        var editorArr = formArr[i].getElementsByTagName("*");
        for (var j = 0; j < editorArr.length; j++) {
            var editor = editorArr[j];
            var className = $(editor).attr('class');
            if (className != null && className.indexOf("required") != -1 &&className.indexOf("x-mo-edit-editor-disabled")<0) {
                $(editor).after("<span style=\"color:red\">*</span>");
            }
        }
    }
}

/*
 根据MO字段名获得组件
 */
function getMoEditorById(id) {
    var formArr = document.getElementsByTagName("form");
    for (var i = 0; i < formArr.length; i++) {
        var editorArr = formArr[i].getElementsByTagName("*");
        for (var j = 0; j < editorArr.length; j++) {
            var editor = editorArr[j];
            if (editor.id.endWith(id)) {
                return editor;
            }
        }
    }
}

/**
 * update endPoints's editor name to match arrayEditor
 */
function updateEndPointsEditorName() {
    $("#endPoints_table").find("tr").each(function(idx) {
        $(this).find("td").each(function() {
            var editor = $(this).children()[0];
            if (editor && editor.name.indexOf("[]") == -1) {
                editor.name = editor.name + "[]";
            }
        });
    });
}


/////////////////////////////////////////////////////////////////////////////
//  ArrayEditor begin
/////////////////////////////////////////////////////////////////////////////

/**
 * 注册数组类型界面组件
 * @param formId
 */
function registArrayEditor(warpId) {
    var arrayEditors = $(".x-gui-arrayeditor");
    if (warpId) {
        arrayEditors = $("#" + warpId + " .x-gui-arrayeditor");
    }
    for (var i = 0; i < arrayEditors.length; i++) {
        var arrayEditor = arrayEditors[i];
        if (!$(arrayEditor).attr("readOnly")) {
            var mode = $(arrayEditor).attr("mode");
            if (!mode) {
                //appendArrayEditorRow($(arrayEditor));
                createArrayEditorRemoveBtn($(arrayEditor));
            }
            updateInnerEditorIdAndName($(arrayEditor));
            if (!mode || (mode && mode == "add")) {
                $(arrayEditor).find("input").each(function() {
                    if (!$(this).attr("readOnly")) {
                        $(this).bind('click', function() {
                            registArrayEditorKeyDownEvent($(arrayEditor), this);
                        });
                    }
                });
            }
        } else {
            $(arrayEditor).find("tr").each(function() {
                $(this).find("td").each(function() {
                    var editors = $(this).children();
                    if (editors.length > 0) {
                        editors[0].readOnly = true;
                    }
                });
            });
        }
    }
}

/**
 * 为数组类型组件注册事件
 * 最后一行获得焦点时自动添加一行
 * @param arrayEditor
 * @param obj
 */
function registArrayEditorKeyDownEvent(arrayEditor, obj) {
    var rows = arrayEditor.find("tr");
    if (rows[rows.length - 1] == obj.parentNode.parentNode) {
        appendArrayEditorRow(arrayEditor);
        createArrayEditorRemoveBtn(arrayEditor);
        updateInnerEditorIdAndName(arrayEditor);
    }
}

/**
 * 添加一行
 * @param arrayEditor
 */
function appendArrayEditorRow(arrayEditor) {
    var rows = arrayEditor.find("tr");
    var cloneObject = $(rows[rows.length - 1]).clone();

    $(cloneObject).find("td").each(function(idx) {
        if (idx == 0) return;
        if(idx == $(cloneObject).find("td").length - 1){
           if($(cloneObject).find("td")[idx].style.display=="none") return;
        }
        var editor = $(this).children()[0];
        $(editor).addClass("ignore");
        editor.setAttribute("array", "true");
        editor.value = "";
        if (editor.tagName == 'input' || editor.tagName == 'INPUT') {
            $(editor).bind("click", function() {
                registArrayEditorKeyDownEvent(arrayEditor, this);
            });
        }
        $(this).find("label").each(function() {
            $(this).remove();
        });
        var editor = $(this).children()[0];
        if (editor) {
            $(editor).removeClass("error");
        }
    });
    arrayEditor.find("tbody").append(cloneObject);
}


/**
 * 创建删除按钮，同时注册事件，修改组件ID和Name
 * @param arrayEditor
 */
function createArrayEditorRemoveBtn(arrayEditor) {
    var rows = arrayEditor.find("tr");
    if (rows.length > 1) {
        for (var i = 0; i < rows.length - 1; i++) {
            var _a = document.createElement("a");
            $(_a).append("X");
            $(_a).bind('click', function() {
                var _tr = $(this).parent("td").parent("tr");
                $(_tr).remove();
            })
            var tds = $(rows[i]).find("td");
            for (var j = 0; j < tds.length; j++) {
                if (j == 0) {
                    $(tds[0]).empty();
                    $(tds[0]).append(_a);
                }
            }
        }
    }
}

var ARRAYEDITOR_EDITORS_IDX = 0;  //下标计数器

/**
 * 创建删除按钮，同时注册事件，修改组件ID和Name
 * @param arrayEditor
 */

function updateInnerEditorIdAndName(arrayEditor) {
    var rows = arrayEditor.find("tr");
    if (rows.length > 1) {
        for (var i = 0; i < rows.length - 1; i++) {
            var tds = $(rows[i]).find("td");
            for (var j = 0; j < tds.length; j++) {
                var tdEditor = $(tds[j]).children()[0];
                if (tdEditor) {
                    $(tdEditor).removeClass("ignore");
                    $(tdEditor).removeAttr("array");

                    tdEditor.id = addIndexForArrayEditorId(tdEditor.id, ARRAYEDITOR_EDITORS_IDX);
                    tdEditor.name = addIndexForArrayEditorName(tdEditor.name, ARRAYEDITOR_EDITORS_IDX);

                    $(tds[j]).find("label").each(function() {
                        this.setAttribute("for", tdEditor.id);
                    });
                }
            }
            ARRAYEDITOR_EDITORS_IDX++;
        }
    }
}


function addIndexForArrayEditorName(string, idx) {
    return string.replace("[]", "[" + idx + "]");
}

function addIndexForArrayEditorId(string, idx) {
    return string+"[" + idx + "]";
}

/////////////////////////////////////////////////////////////////////////////
//  ArrayEditor end
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// gui visiable
////////////////////////////////////////////////////////////////////////////

/**
 * 注册Gui的Field是否可见
 */
function registFieldVisiable() {
    var formArr = document.getElementsByTagName("form");
    for (var i = 0; i < formArr.length; i++) {
        var editorArr = formArr[i].getElementsByTagName("*");
        for (var j = 0; j < editorArr.length; j++) {
            var editor = editorArr[j];
            var expression = $(editor).attr('visibleEx');
            if (expression) {
                var expressions = expression.split(",");
                var noRepeats = clearRepeatVisiableExpression(expressions);
                for (var k = 0; k < noRepeats.length; k++) {
                    registVisiableEventByExpression(noRepeats[k], editor);
                }
            }
        }
    }
}

/**
 * 消除重复的表达式
 * @param expressions 表达式
 */
function clearRepeatVisiableExpression(expressions) {
    //the expressions data is ["${Version_Get} == 'SNMPV1'","${Version_Get} == 'SNMPV2'"]
    //i want result is [[Version_Get,[== 'SNMPV1',== 'SNMPV2']]]
    var noRepeats = new Array();
    for (var i = 0; i < expressions.length; i++) {
        /\$\{(.+)\}(.+)/.test(expressions[i]);
        var subjectId = RegExp.$1;
        var condition = RegExp.$2;
        var isNew = true;
        for (var j = 0; j < noRepeats.length; j++) {
            if (noRepeats[j][0] == subjectId) {
                var conditions = noRepeats[j][1];
                conditions[conditions.length] = condition;
                isNew = false;
                break;
            }
        }
        if (isNew) {
            var expression = new Array();
            var condtions = new Array();
            expression[0] = subjectId;
            condtions[0] = condition;
            expression[1] = condtions;
            noRepeats[i] = expression;
        }
    }
    return noRepeats;
}

/**
 * 为事件触发方注册事件
 * @param expression
 * @param observer
 */
function registVisiableEventByExpression(expression, observer) {

    var srcEditorId = expression[0];
    var conditions = expression[1];
    if (srcEditorId && srcEditorId != '') {
        var subject = getMoEditorById("_" + srcEditorId);
        guiVisiable(subject, observer, conditions);
        $(subject).bind("change", function() {
            guiVisiable(subject, observer, conditions);
        });
    }
}

/**
 * 控制是否可见
 * @param subject 主题
 * @param observer  观察者
 * @param conditions 条件
 */
function guiVisiable(subject, observer, conditions) {
    var isVisiables = new Array();
    for (var i = 0; i < conditions.length; i++) {
        isVisiables[i] = eval("'" + subject.value + "'" + conditions[i]);
    }
    var isVisiable = eval(isVisiables.join("|"));
    var status = isVisiable ? "visible" : "hidden";
    observer.style.visibility = status;
    $("label[for='" + observer.id + "']").each(function() {
        this.style.visibility = status;
    })
}


//////////////////////////////////////////////////////////////////////
//Mo's Optional Access parameters Style
//////////////////////////////////////////////////////////////////////
/**
 * 注册访问参数是否展开
 */
function registOptionalAccessParamStyle(initType) {
    $("#accesses fieldset[id]").each(function (){
        var legend = $(this).find(".group-legend").first();
        legend.addClass("ui-block-state-editable");
        if(legend.attr("suppressible") === "false"){
            legend.addClass("ui-block-state-fixed");
        }
        else {
            if(legend.attr("suppressed") == "true")
                legend.addClass("ui-block-state-collapsed");

            var optionalId = legend.attr("optionalId");
            var targetAccessParam = getMoEditorById(optionalId);
            var editType = typeof accessesEditType == 'undefined' ? -1 : accessesEditType[optionalId];

            updateAccessVisibility(legend, targetAccessParam, editType);
            legend.bind("click", function(){
                $(this).toggleClass("ui-block-state-collapsed");
                updateAccessVisibility($(this), targetAccessParam, $(this).hasClass("ui-block-state-collapsed") ? -1 : 1);
            })
        }
    });
    $(".mo-accessparameter-optional-btn").each(function() {
//        var optionalId = $(this).attr("optionalId");
//        var targetAccessParam = getMoEditorById(optionalId);
//        var editType = typeof accessesEditType == 'undefined' ? -1 : accessesEditType[optionalId];
//        if (editType != -1)$(this).attr('checked', 'checked');
//        turnMoOptionalApStatus(this, targetAccessParam, editType);
//        $(this).bind("click", function() {
//            turnMoOptionalApStatus(this, targetAccessParam, $(this).attr('checked') == 'checked' ? 1 : -1);
//        });
    })
}


function updateAccessVisibility(legend,targetAccessParam, editType){
    var container = legend.parent().next();
    container.css("display", legend.hasClass("ui-block-state-collapsed") ? "none" : "block");
    turnMoOptionalApStatus(legend,targetAccessParam,editType);
}

/**
 * 切换访问参数展开状态
 * @param eventBtn  事件触发者
 * @param targetAccessParam 访问参数界面对象
 * @param isCollapse 是否展开
 */
function turnMoOptionalApStatus(legend, targetAccessParam, editType) {
    legend.removeClass("x-mo-edit-editor-disabled");

    if (editType == 1 || editType == 0) {
        legend.attr("value", "true");
        $(targetAccessParam).find("select").each(function() {
            if (!$(this).attr("array")) {
                $(this).removeClass("ignore");
            }
        });
        $(targetAccessParam).find("input").each(function() {
            if (!$(this).attr("array")) {
                $(this).removeClass("ignore");
            }
        });

        if (editType == 0)//on view page
            legend.addClass("x-mo-edit-editor-disabled");
    }
    else if (editType == -1) {//not visible, disable validation
        legend.attr("value", "false");
        $(targetAccessParam).find("select").each(function() {
            $(this).addClass("ignore");
        });
        $(targetAccessParam).find("input").each(function() {
            $(this).addClass("ignore");
        });

    }
}

////////////////////////////////////////////////////////////////////////
// 创建访问参数测试按钮
///////////////////////////////////////////////////////////////////////
/**
 * 创建访问参数测试按钮
 */
function createAccessParaTestBtn(initType) {
    //var moId = $("#moId").attr("value");
    //if (!moId) return;
    $("fieldset[id^=PSManagedObject_Accesses]").each(function() {
        var accessParaId = $(this).attr("id");
        if (accessParaId.split("_").length == 3) {
            var accessPara = accessParaId.split("_")[2];
            var accessParaTest = $("#" + accessPara.toLowerCase() + "_Test").attr("value");
            var accessName = $("#" + accessPara.toLowerCase() + "_Test").attr("name");
            if (!accessParaTest)accessParaTest = "";
            var legend = $(this).children("legend")[0];
//            var btnSpan = legend.getElementsByTagName("span")[0];
//            if (btnSpan) {
//                legend.removeChild(btnSpan);
//            }
            var isEnable = true;
            $(".mo-accessparameter-optional-btn").each(function() {
                var optionalAccessParaName = $(this).attr("optionalid");
                if (optionalAccessParaName == accessPara) {
                    isEnable = $(this).attr("value") == "true";
                }
            });

            if (legend && isEnable && accessParaTest != "" && accessParaTest != "null" && $(legend).text().indexOf(l("mo_text_test")) <= 0) {
                var method;
                if (initType == "test_access") {
                    method = "javascript:testAccesses('" + accessName + "');";
                } else {
                    method = "javascript:getIndicatorValue('" + accessName + "');"
                }

                var button = String.formatModel($.ui.button.buttonTemplate, {id:'',cls:'',action:method,label:l("mo_text_test")});
                $(legend).append(button);
            }
        }
    });
}


/////////////////////////////////////////////////////////////////////////
// add or edit mo
/////////////////////////////////////////////////////////////////////////
/**
 * create or update simple mo
 * @param ajaxUrl targetUrl
 * @param data mo's data
 * @param counts mos's counts
 */
function ajaxMoOverLay(ajaxUrl, data, counts) {
    var completeHTML = l("mo_text_operation_finished");
    ajaxOverLayForProbe(ajaxUrl, "POST", data, counts,
                completeHTML,
                [
                    {text:l("mo_text_back"),handler:'location.href = \"/mos\"'},
                    {text:l("mo_text_close"),handler:'return true;'}
                ]);
}


function saveMultiMo(url, retrieve, title, method) {
    var executor = $.orderlyTaskExecutor.create({title:title,
                                                    endTip:l("mo_text_operation_finished"),
                                                    actions:[
                                                        {text:l("mo_text_back"),handler:'location.href = \"/mos\"'},
                                                        {text:l("mo_text_close"),handler:'return true;'}
                                                    ]
                                                });

    var mo_data_arr = getEndPoints();
    for (var i = 0; i < mo_data_arr.length; i++) {
        var endPointsParamStr = mo_data_arr[i][1];
        var paramStr = getMoData() + endPointsParamStr + "retrieve=" + retrieve;
        executor.addTask({
                             url:url,
                             method:method,paramsStr:paramStr
                         });
    }

    executor.start();
}
/**
 * batch save
 */
function saveMultiMoWithId(url,ids, retrieve, title, method) {
    var idarray=ids.split(',');
    var executor = $.orderlyTaskExecutor.create({title:title,
                                                    endTip:l("mo_text_operation_finished"),
                                                    actions:[
                                                        {text:l("mo_text_back"),handler:'location.href = \"/mos\"'},
                                                        {text:l("mo_text_close"),handler:'return true;'}
                                                    ]
                                                });

    var mo_data_arr = getEndPoints();
    for (var i = 0; i < mo_data_arr.length; i++) {
        var endPointsParamStr = mo_data_arr[i][1];
        var paramStr = getMoData() + endPointsParamStr + "retrieve=" + retrieve;
        executor.addTask({
                             url:url+idarray[i],
                             method:method,paramsStr:paramStr
                         });
    }

    executor.start();
}
var mo_count = 0;
function getEndPoints() {
    mo_count = 0;
    var mo_data_arr = new Array();
    var trs = $("#endPoints_table").find("tr");
    trs.each(function(idx) {
        if (!(trs.length - 1 == idx && $("#endPoints_table").attr("mode") == 'add')) {
            var endPointsParamStr = "";
            $(this).find("td").each(function(i) {
                var editor = $(this).children()[0];
                if (editor) {
                    var key = editor.name.replaceAll("\[[0-9]*\]", "");
                    var tmpIdx = editor.name.lastIndexOf("@");
                    if (tmpIdx != -1)key = editor.name.substring(0, tmpIdx);
                    //if (editor.value && editor.value.trim() != "") {
                    endPointsParamStr += key + "=";
                    endPointsParamStr += editor.value + "&";
                    //}
                }
            });
            if (endPointsParamStr != "") {
                var mos = new Array();
                mos[0] = idx + 1;
                mos[1] = endPointsParamStr;
                mo_data_arr[mo_count] = mos;
                mo_count++;
            }
        }
    });
    return mo_data_arr;
}
/**
 * get mo's data
 */
function getMoData() {
    var dataStr = "";
    $("#moform").find("*").each(function() {
        var name = $(this).attr("name");
        if (name) {
            var value = $(this).attr("value");
            var classes = $(this).attr("class");
            if (value && value.trim() != "" && (name.indexOf("moType") == 0
                    || name.indexOf("PSManagedObject") == 0)
                    || name.indexOf("optional") == 0
                    || name == "EndPoint/Name") {
                if (!$(this).hasClass("x-mo-edit-editor-disabled") && $(this).css('visibility') != 'hidden') {
                    dataStr += name + "=";
                    dataStr += value + "&";
                }
            }
        }
    });
    return dataStr;
}

function base64Access() {
    $("fieldset[id^=PSManagedObject_Accesses]").each(function() {
        var accessParaId = $(this).attr("id");
        if (accessParaId.split("_").length == 3) {
            var accessPara = accessParaId.split("_")[2];
            var accessName = $("#" + accessPara.toLowerCase() + "_Test").attr("name");
            var parentID = "PSManagedObject_Accesses_" + accessName;
            var basename = "PSManagedObject_Accesses/";
            $("#" + parentID).find("*").each(function() {
                var name = $(this).attr("name");
                if (name && name.indexOf(basename) == 0) {
                    var value = $(this).attr('value');
                    if (value && value != "" && !$(this).hasClass('x-mo-edit-editor-disabled')) {
                        if (name && name.indexOf("PWD") > 0
                            || name.indexOf("Password") > 0
                            ||name.indexOf("Community_Set") > 0
                            ||name.indexOf("Community_Get") > 0
                            ||name.indexOf("AuthPassword_Get") > 0
                            ||name.indexOf("PrivPassword_Get") > 0
                            ||name.indexOf("AuthPassword_Set") > 0
                            ||name.indexOf("PrivPassword_Set") > 0
                            ||name.indexOf("Password1") > 0
                            ||name.indexOf("Password2") > 0) {
                            var base64 = new Base64();
                            $(this).attr('value',base64.encode(value));
                        }
                    }
                }
            });
        }
    });
}

function getAccesses(accessName) {
    var parentID = "PSManagedObject_Accesses_" + accessName;
    var basename = "PSManagedObject_Accesses/";
    var data = "accessName=" + $("#" + accessName.toLowerCase() + "_Test").attr("name");
    $("#" + parentID).find("*").each(function() {
        var name = $(this).attr("name");
        if (name && name.indexOf(basename) == 0) {
            var value = $(this).attr('value');
            if (value && value != "" && !$(this).hasClass('x-mo-edit-editor-disabled')) {
                var base64 = new Base64();
                data += "&";
                data += name;
                data += "=";
                if (name && name.indexOf("PWD") > 0) {
                    data += base64.encode(encodeURIComponent(value));
                } else if(name && name.indexOf("Password") > 0||name.indexOf("Community_Set") > 0
                    ||name.indexOf("Community_Get") > 0||name.indexOf("AuthPassword_Get") > 0
                    ||name.indexOf("PrivPassword_Get") > 0||name.indexOf("AuthPassword_Set") > 0
                    ||name.indexOf("PrivPassword_Set") > 0||name.indexOf("Password1") > 0
                    ||name.indexOf("Password2") > 0) {
                    data += base64.encode(value);
                }else{
                    data += value;
                }
            }
        }
    });
    return data;
}

////////////////////////////////////////////////////////////////
//show mo
////////////////////////////////////////////////////////////////
function testAccesses(accessName) {
    overLay(accessName+l("mo_text_snmp_test"));
    var moType = $("#moType").attr("value");

    var mo_data_arr = getEndPoints();
    if(mo_data_arr.length!=null&&mo_data_arr.length!=0){
    for (var i = 0; i < mo_data_arr.length; i++) {
        var idx = mo_data_arr[i][0];
        var endPointsParamStr = mo_data_arr[i][1];
        ajaxMoOverLay("/mos/" + moType + "/testAccesses", getAccesses(accessName) + "&" + endPointsParamStr,
                      mo_count);
    }}
    else{
       ajaxMoOverLay("/mos/" + moType + "/testAccesses", getAccesses(accessName),
                      ++mo_count);
    }
    mo_count = 0;
}

function getMultiIndicatorValue(indicatorName) {

}

function getIndicatorValue(indicatorName, actionType) {
    var title = actionType == "indicator" ? l("mo_text_get_indicator") : l("mo_text_perform_operation");
    overLay(title);
    var moId = $("#moId").attr("value");
    ajaxMoOverLay("/mos/" + moId + "/actions/" + actionType + "/" + indicatorName, "", 1);
}

function gotoGetIndicatorValue(indicatorName, actionType) {
    var title = actionType == "indicator" ? l("mo_text_get_indicator") : l("mo_text_perform_operation");
    overLay(title);
    var moId = $("#moId").attr("value");
    if(actionType=="indicator")
    ajaxOverLay("/mos/" + moId + "/" + indicatorName + "/indicatorParams", "GET", "indicatorName=" + indicatorName, 1, "",
                [{text:l("mo_text_ok"),handler:function() {
                        $("#indicatorParamsForm").submit();
                    }
                },
                 {text:l("mo_text_close"),handler:'return true;'}], function() {
        $("#indicatorParamsForm").validate({
                                               ignore : ".ignore",
                                               submitHandler: function() {
                                                   getIndicatorValueByParams(indicatorName, actionType);
                                               }
                                           });
        registArrayEditor("indicatorParamsForm");
    });
    else{
      ajaxOverLay("/mos/" + moId + "/" + indicatorName + "/operationParams", "GET", "operationParams=" + indicatorName, 1, "",
                [{text:l("mo_text_ok"),handler:function() {
                        $("#indicatorParamsForm").submit();
                    }
                },
                 {text:l("mo_text_close"),handler:'return true;'}], function() {
        $("#indicatorParamsForm").validate({
                                               ignore : ".ignore",
                                               submitHandler: function() {
                                                   getIndicatorValueByParams(indicatorName, actionType);
                                               }
                                           });
        registArrayEditor("indicatorParamsForm");
    });
    }
}

function getIndicatorValueByParams(indicatorName, actionType) {
    var moId = $("#moId").attr("value");
    var data = "indicatorName=" + indicatorName;
    $("#indicatorParamsForm").find("*").each(function() {
        var name = $(this).attr("name");
        if (name && name.indexOf("PSManagedObject_indicatorParams/") == 0) {
            var value = $(this).attr("value");
            if (value && value != "") {
                data += "&";
                data += name;
                data += "=";
                data += value;
            }
        }
    });
    $("body").data("dialog").close();
    var title = actionType == "indicator" ? l("mo_text_get_indicator") : l("mo_text_perform_operation");
    overLay(title);
    ajaxMoOverLay("/mos/" + moId + "/actions/" + actionType + "/" + indicatorName, data, 1);
}

//$(function() {
//    $(".ui-seg-title").each(function() {
//        $(this).bind("click", function(event) {
//            if (!$(event.target).attr("href")) {
//                !$(this).parent().toggleClass("ui-seg-collapse")
//                if (!$(this).parent().hasClass("ui-seg-collapse"))
//                    $(this).siblings().removeClass("ui-hide");
//                else
//                    $(this).siblings().addClass("ui-hide");
//            }
//        });
//    });
//});